﻿using System;
using System.Collections.Generic;
using System.Text;
using ToDoList.Logic.DataSource;
using System.Linq;

namespace ToDoList.Logic.Service
{
    public class MatterService
    {
        // 添加备注
        public void AddNote(Matters matter, string note)
        {
            if (IsThisMatterExisted(matter.UserId, matter.InventoryId, matter.MatterID))
            {
                matter.Note = note;
            }
            else
                throw new Exception("This matter didn't exist");
        }

        // 添加过期时间
        public void SetDeadline(Matters matter, int year, int month, int day)
        {
            if (IsThisMatterExisted(matter.UserId, matter.InventoryId, matter.MatterID))
            {
                DateTime deadline = new DateTime(year, month, day);
                matter.Deadline = deadline;
            }
            else
                throw new Exception("This matte didn't exist");

        }

        // 完成事项
        public void DoneIt(Matters matter)
        {
            if (IsThisMatterExisted(matter.UserID, matter.InventoryId, matter.MatterID))
            {
                matter.IsDone = true;
            }
            else
                throw new Exception("This matter didn't exist");
        }

        // 取消完成事项
        public void CancelDoneIt(Matters matter)
        {
            if (IsThisMatterExisted(matter.UserID, matter.InventoryId, matter.MatterID))
            {
                matter.IsDone = false;
            }
            else
                throw new Exception("This matter didn't exist");
        }

        // 找到指定的事项
        public static Matters FindMatter(int userID, int inventoryID, int matterID)
        {
            Inventories inventory = InventoryService.FindInventory(userID, inventoryID);
            Matters matter = inventory.Matters.SingleOrDefault(m => m.MatterID == matterID);
            if (matter == null)
                throw new Exception("This matter isn't exist");
            else
                return matter;
        }

        // 指定的事项是否存在
        public static bool IsThisMatterExisted(int userID, int inventoryID, int matterID)
        {
            Inventories inventory = InventoryService.FindInventory(userID, inventoryID);
            return inventory.Matters.Any(matter => matter.MatterID == matterID);
        }
    }
}
